#include "head.h"
#include "config.h"
#include "user_login.h"

int main(int argc,char* argv[])
{
    // ./client.c
    if(argc > 2) {
        fprintf(stderr, "ERROR: Invalid arguments.\n");
        exit(1);
    }
    // 读取配置文件
    config_t configs[50];
    bzero(configs, sizeof(configs));
    int config_count;
    if(argc == 2) {
        read_conf(argv[1], configs, &config_count);
    } else {
        read_conf("../conf/server.conf", configs, &config_count);
    }

    char ip[20] = {0};
    char port[10] = {0};

    get_conf_value(configs, config_count, "ip", ip);
    get_conf_value(configs, config_count, "port", port);

    //tcp连接
    int sockfd = tcpInit(ip, port);

    free_configs(configs, config_count);

    //登录界面
    printf("===== 连接到服务器 =====\n");
    printf("===== IP = %s =====\n",ip);
    printf("===== PORT = %s ======\n",port);
    printf("\n");

    char output[4096] = {0};//输出字符串用
    //int login_flag = -1;
    cmd_t cmdInfo;
    user_t user;
    train_t train;
    char path[256] = "~";

    user_login(sockfd,&cmdInfo,&user);

    printf("登录成功！\n");
    printf("[%s @%s-netdisk]:%s$ ",user.name,user.name,path);
    fflush(stdout);

    fd_set rdset;
    while(1){
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(sockfd,&rdset);
        select(sockfd+1,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(STDIN_FILENO,&rdset)){
            char cmd[1024] = {0};
            cmd_t cmdInfo;
            bzero(&cmdInfo,sizeof(cmdInfo));
            char* argv[5] = {0};
            int argc;


            int ret = read(STDIN_FILENO,cmd,256);
            ERROR_CHECK(ret,-1,"read");
            cmd[strlen(cmd) - 1] = '\0';

            cmd_separate(cmd,argv,&argc);
            strncpy(cmdInfo.cmd,argv[0],strlen(argv[0]));
            if(argc != 1){
                strncpy(cmdInfo.args,argv[1],strlen(argv[1]));
            }
            //判断cmd是否正确
            if(is_cmd(cmdInfo.cmd) == -1){
                printf("ERROR CMD\n");
                printf("[%s @%s-netdisk]:%s$ ",user.name,user.name,path);
                fflush(stdout);
                continue;
            }
            /* printf("op = %s\n",cmdInfo.cmd); */
            /* printf("ob = %s\n",cmdInfo.args); */

            bzero(&train,sizeof(train_t));
            train.length = sizeof(cmdInfo);
            memcpy(train.data,&cmdInfo,sizeof(cmdInfo));
            send_msg(sockfd,&train);

            //下载文件
            if (strcmp(cmdInfo.cmd, "gets") == 0) {
                //发参数
                train_t train_arg;
                bzero(&train_arg, sizeof(train_arg));
                train_arg.length = strlen(cmdInfo.args);
                memcpy(train_arg.data, cmdInfo.args, train_arg.length);
                send_msg(sockfd, &train_arg);
                cTransFile(sockfd, RECVF, cmdInfo.args);

                printf("[%s @%s-netdisk]:%s$ ",user.name,user.name,path);
                fflush(stdout);
            }
            //上传文件
            else if (strcmp(cmdInfo.cmd, "puts") == 0) {
                //发参数
                train_t train_arg;
                bzero(&train_arg, sizeof(train_arg));
                train_arg.length = strlen(cmdInfo.args);
                memcpy(train_arg.data, cmdInfo.args, train_arg.length);
                send_msg(sockfd, &train_arg);
                cTransFile(sockfd, SENDF, cmdInfo.args);

                printf("[%s @%s-netdisk]:%s$ ",user.name,user.name,path);
                fflush(stdout);
            }
            else if(strcmp(cmdInfo.cmd,"cd") == 0){
                bzero(&output,sizeof(output));
                //接收返回的字符串
                int ret = recv_msg(sockfd,&train);
                if(ret == 0){
                    close(sockfd);
                    return 0;
                }
                memcpy(output,train.data,train.length);
                /* printf("%s\n",output); */

                char *p;
                p = strtok(output,"/");
                char *p1 = strtok(NULL,"/");
                if(p1 == NULL){
                    strcpy(path,p);
                }
                else{
                    strcpy(path,p1);
                    while((p = strtok(NULL,"/")) != NULL){
                        strcpy(path,p);
                    }
                }

                /* printf("<------------>\n"); */
                printf("[%s @%s-netdisk]:%s$ ",user.name,user.name,path);
                fflush(stdout);
            }


        }
        if(FD_ISSET(sockfd,&rdset)){
            bzero(&output,sizeof(output));
            //接收返回的字符串
            int ret = recv_msg(sockfd,&train);
            if(ret == 0){
                close(sockfd);
                return 0;
            }

            memcpy(output,train.data,train.length);
            printf("%s\n",output);

            /* printf("<------------>\n"); */
            printf("[%s @%s-netdisk]:%s$ ",user.name,user.name,path);
            fflush(stdout);
        }
    }
    return 0;
}
